package com.mce.nebula.entity;

import com.mce.nebula.common.BaseEntity;
import com.mce.nebula.enums.AppUpgradeRecordType;
import com.mce.nebula.enums.UpgradeStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 云市应用升级记录实体，对应Python的AppUpgradeRecord模型
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "app_upgrade_record")
public class AppUpgradeRecord extends BaseEntity {
    
    @Column(name = "tenant_id", length = 33, nullable = false)
    private String tenantId;
    
    @Column(name = "group_id", nullable = false)
    private Integer groupId;
    
    @Column(name = "group_key", length = 32, nullable = false)
    private String groupKey;
    
    @Column(name = "group_name", length = 64, nullable = false)
    private String groupName;
    
    @Column(name = "version", length = 20)
    private String version;
    
    @Column(name = "old_version", length = 20)
    private String oldVersion;
    
    @Column(name = "status", nullable = false)
    private Integer status = UpgradeStatus.NOT.getValue();
    
    @Column(name = "market_name", length = 64)
    private String marketName;
    
    @Column(name = "is_from_cloud", nullable = false)
    private Boolean isFromCloud = false;
    
    @Column(name = "upgrade_group_id", nullable = false)
    private Integer upgradeGroupId = 0;
    
    @Column(name = "snapshot_id", length = 32)
    private String snapshotId;
    
    @Column(name = "record_type", length = 64)
    private String recordType;
    
    @Column(name = "parent_id", nullable = false)
    private Integer parentId = 0;
    
    /**
     * 判断是否可以回滚
     */
    public boolean canRollback() {
        if (!AppUpgradeRecordType.UPGRADE.getValue().equals(this.recordType)) {
            return false;
        }
        int[] rollbackableStatuses = {
            UpgradeStatus.UPGRADED.getValue(),
            UpgradeStatus.ROLLBACK.getValue(),
            UpgradeStatus.PARTIAL_UPGRADED.getValue(),
            UpgradeStatus.PARTIAL_ROLLBACK.getValue(),
            UpgradeStatus.DEPLOY_FAILED.getValue()
        };
        
        for (int status : rollbackableStatuses) {
            if (this.status == status) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 判断是否已完成
     */
    public boolean isFinished() {
        return this.status != UpgradeStatus.NOT.getValue() && 
               this.status != UpgradeStatus.UPGRADING.getValue() && 
               this.status != UpgradeStatus.ROLLING.getValue();
    }
    
    /**
     * 判断是否可以升级
     */
    public boolean canUpgrade() {
        return this.status == UpgradeStatus.NOT.getValue();
    }
    
    /**
     * 判断是否可以部署
     */
    public boolean canDeploy() {
        if (!isFinished()) {
            return false;
        }
        int[] deployableStatuses = {
            UpgradeStatus.UPGRADE_FAILED.getValue(),
            UpgradeStatus.ROLLBACK_FAILED.getValue(),
            UpgradeStatus.PARTIAL_UPGRADED.getValue(),
            UpgradeStatus.PARTIAL_ROLLBACK.getValue(),
            UpgradeStatus.DEPLOY_FAILED.getValue()
        };
        
        for (int status : deployableStatuses) {
            if (this.status == status) {
                return true;
            }
        }
        return false;
    }
}